//
//  UIViewController+Ext.m
//  mf
//
//  Created by JCai on 2017/4/23.
//  Copyright © 2017年 fenmi. All rights reserved.
//

#import "UIViewController+Ext.h"
#import "LoginBySecretView.h"
#import "UIImage+GIF.h"

#define kScaling  ([[UIScreen mainScreen] bounds].size.width / 375.0f)

@implementation UIViewController (UINavigationController)

#pragma mark - 设置透明上导航
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (void)setNavColor:(UIColor *)navColor
{
    UIImage *image = [self imageWithColor:navColor];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:40/255.0f green:38/255.0f blue:43/255.0f alpha:1.0f]];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

- (void)setNavColour:(UIColor *)navColor
{
    UIImage *image = [self imageWithColor:navColor];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:40/255.0f green:38/255.0f blue:43/255.0f alpha:1.0f]];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

#pragma mark - 设置标题
- (void)setNavTitle:(NSString *)title
{
    UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100*kScaling, 30*kScaling)];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:NSTextAlignmentCenter];
    [tempLabel setTextColor:[UIColor blackColor]];
    [tempLabel setFont:[UIFont systemFontOfSize:18.0f*kScaling]];
    [tempLabel setText:title];
    self.navigationItem.titleView = tempLabel;
}

#pragma mark - 
- (void)setNavBack
{
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    CGRect tempFrame = CGRectMake(0, 0, 20*kScaling, 32*kScaling);
    UIButton *tempButton = [[UIButton alloc] initWithFrame:tempFrame];
    [tempButton addTarget:self
                   action:@selector(backToLast)
         forControlEvents:UIControlEventTouchUpInside];
    [tempButton setImage:[UIImage imageNamed:@"CocachBackImg"]
                          forState:UIControlStateNormal];
    [tempButton setImageEdgeInsets:UIEdgeInsetsMake(7*kScaling, 0, 7*kScaling, 10*kScaling)];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
- (void)setNavBackController
{
    CGRect tempFrame = CGRectMake(0, 0, 16*kScaling, 28*kScaling);
    UIButton *tempButton = [[UIButton alloc] initWithFrame:tempFrame];
    [tempButton addTarget:self
                   action:@selector(backToLastController)
         forControlEvents:UIControlEventTouchUpInside];
    [tempButton setBackgroundImage:[UIImage imageNamed:@"back_image.png"]
                          forState:UIControlStateNormal];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)backToLastController
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark -
- (void)setNavNull
{
//    [self setNavLeftBarButton:CGRectMake(0, 0, 20, 20)
//                        title:@""
//                       action:nil
//                  normalImage:nil
//               highlightImage:nil];
}

#pragma mark - 设置导航按钮
- (void)setNavRightBarButton:(CGRect)frame
                       title:(NSString *)title
                      action:(SEL)action
                 normalImage:(UIImage *)normalImage
              highlightImage:(UIImage *)highlightImage WithJudge:(BOOL)judge
{
    CGRect tempFrame = frame;
    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tempButton.frame = tempFrame;
    [tempButton setImageEdgeInsets:UIEdgeInsetsMake(10*SCALEUNIT, 10*SCALEUNIT, 10*SCALEUNIT, 10*SCALEUNIT)];
    [tempButton.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-2]];
    [tempButton setContentHorizontalAlignment:judge?UIControlContentHorizontalAlignmentRight:UIControlContentHorizontalAlignmentLeft];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton setTitleColor:RGBA(70, 70, 70, 1) forState:UIControlStateNormal];
    [tempButton addTarget:self
                   action:action
         forControlEvents:UIControlEventTouchUpInside];
    if (normalImage) {
        [tempButton setBackgroundImage:normalImage
                              forState:UIControlStateNormal];
    }
    if (highlightImage) {
        [tempButton setBackgroundImage:highlightImage
                              forState:UIControlStateHighlighted];
    }
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
    if (judge) {
        self.navigationItem.rightBarButtonItem = barButton;
    }else {
        self.navigationItem.leftBarButtonItem = barButton;
    }
}

#pragma mark 设置导航文本按钮
- (void)setNavBarButton:(NSString *)title
                      action:(SEL)action WithJudge:(BOOL)judge
{
    [self setNavRightBarButton:CGRectMake(0, 0, 30 * kScaling, 20 * kScaling)
                         title:title
                        action:action
                   normalImage:nil
                highlightImage:nil WithJudge:judge];
}

- (void)setBackImage:(UIImage *)backImage
{
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    [backImageView setImage:backImage];
    [self.view addSubview:backImageView];
}

#pragma mark - 提示未登录弹框
- (void)remindToLogin:(BOOL)judge{//judge==YES查看内容    judge==NO使用功能
    
    NSString *messageStr;
    if (judge) {
        messageStr = @"查看该模块内容需要您先登录,是否跳转到登录界面";
    }else{
        messageStr = @"使用该功能需要您先登录,是否跳转到登录界面";
    }
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:messageStr preferredStyle:UIAlertControllerStyleAlert];
    [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //[self.navigationController pushViewController:[[LoginBySecretView alloc]init] animated:YES];
    }]];
    [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alertC animated:YES completion:nil];
}

@end
